using Core;
using Microsoft.AspNetCore.Mvc;
using Server.Responses;

namespace Server.Controllers;

[ApiController]
[Route("[controller]")]
public class OrderController : ControllerBase
{
    private readonly PlayServer _server;
    private readonly ILogger<OrderController> _logger;

    public OrderController(PlayServer server, ILogger<OrderController> logger)
    {
        _logger = logger;
        _server = server;
    }

    [HttpPost]
    [Route("Add")]
    public async ValueTask<OrderResponse> AddOrder([FromBody] OrderRequest request)
    {
        try
        {
            var response = await _server.AddOrderAsync(new CommandOrder
            {
                TargetId = request.TargetId,
            });

            return new OrderResponse
            {
                SuccessFul = response.SuccessFul,
            };
        }
        catch (TimeoutException e)
        {
            return new OrderResponse
            {
                SuccessFul = false,
                ErrorMessage = "操作超时"
            };
        }
    }
}